// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) definits fieldsfirst ansi space safe 
// Source File Name:   SimulationView.java

package jones.gui;

import java.awt.*;
import java.awt.event.*;
import java.io.PrintStream;
import java.util.Observable;
import javax.swing.JPanel;
import javax.swing.OverlayLayout;
import jones.*;
import jones.rigid.RigidBody;

// Referenced classes of package jones.gui:
//            LightView, RobotView, SensorPanel, WallView, 
//            RigidBodyView

public class SimulationView extends JPanel
{

    public static final int WIDTH = 574;
    public static final int HEIGHT = 544;
    private Simulation simulation = null;
    private JPanel sensorPanels = null;
    private int robotIndex = 0;

    public SimulationView(Simulation simulation)
    {
        sensorPanels = new JPanel(((java.awt.LayoutManager) (new CardLayout())));
        robotIndex = 0;
        this.simulation = simulation;
        setLayout(((java.awt.LayoutManager) (new OverlayLayout(((java.awt.Container) (this))))));
        setOpaque(true);
        setBackground(Color.black);
        setCursor(Cursor.getPredefinedCursor(1));
        addMouseListener(((java.awt.event.MouseListener) (new MouseAdapter() {

            public void mouseClicked(MouseEvent e)
            {
                System.out.println("SimulationView clicked");
            }

        }
)));
        initViews();
    }

    private void initViews()
    {
        Observable observables[] = simulation.getObservables();
        for (int i = 0; i < observables.length; i++)
            if (observables[i] instanceof Light)
            {
                final Light light = (Light)observables[i];
                final LightView lightView = new LightView(light);
                lightView.addKeyListener(((java.awt.event.KeyListener) (new KeyAdapter() {

                    public void keyPressed(KeyEvent e)
                    {
                        if (e.getKeyCode() == 127 || e.getKeyCode() == 8)
                        {
                            simulation.removeObservable(((Observable) (light)));
                            remove(((java.awt.Component) (lightView)));
                            revalidate();
                            repaint();
                        }
                    }

                }
)));
                observables[i].addObserver(((java.util.Observer) (lightView)));
                add(((java.awt.Component) (lightView)));
            } else
            if (observables[i] instanceof SimpleRobot)
            {
                final SimpleRobot robot = (SimpleRobot)observables[i];
                final RobotView robotView = new RobotView(robot, simulation.getWorld());
                final SensorPanel sensorPanel = new SensorPanel(robot);
                final String cardString = getNewCardString();
                robotView.addMouseListener(((java.awt.event.MouseListener) (new MouseAdapter() {

                    public void mousePressed(MouseEvent e)
                    {
                        CardLayout cardLayout = (CardLayout)sensorPanels.getLayout();
                        cardLayout.show(((java.awt.Container) (sensorPanels)), cardString);
                    }

                }
)));
                robotView.addKeyListener(((java.awt.event.KeyListener) (new KeyAdapter() {

                    public void keyPressed(KeyEvent e)
                    {
                        if (e.getKeyCode() == 127 || e.getKeyCode() == 8)
                        {
                            simulation.removeObservable(((Observable) (robot)));
                            sensorPanels.remove(((java.awt.Component) (sensorPanel)));
                            sensorPanels.revalidate();
                            sensorPanels.repaint();
                            remove(((java.awt.Component) (robotView)));
                            revalidate();
                            repaint();
                        }
                    }

                }
)));
                sensorPanels.add(((java.awt.Component) (sensorPanel)), ((Object) (cardString)));
                add(((java.awt.Component) (robotView)));
                robot.addObserver(((java.util.Observer) (robotView)));
                CardLayout cardLayout = (CardLayout)sensorPanels.getLayout();
                cardLayout.show(((java.awt.Container) (sensorPanels)), cardString);
            } else
            if (observables[i] instanceof Wall)
            {
                final Wall wall = (Wall)observables[i];
                final WallView wallView = new WallView(wall, simulation.getWorld());
                wallView.addKeyListener(((java.awt.event.KeyListener) (new KeyAdapter() {

                    public void keyPressed(KeyEvent e)
                    {
                        if (e.getKeyCode() == 127 || e.getKeyCode() == 8)
                        {
                            simulation.removeObservable(((Observable) (wall)));
                            remove(((java.awt.Component) (wallView)));
                            revalidate();
                            repaint();
                        }
                    }

                }
)));
                wall.addObserver(((java.util.Observer) (wallView)));
                add(((java.awt.Component) (wallView)));
            } else
            if (observables[i] instanceof RigidBody)
            {
                final RigidBody rigidBody = (RigidBody)observables[i];
                final RigidBodyView rigidBodyView = new RigidBodyView((RigidBody)observables[i], simulation.getWorld());
                rigidBodyView.addKeyListener(((java.awt.event.KeyListener) (new KeyAdapter() {

                    public void keyPressed(KeyEvent e)
                    {
                        if (e.getKeyCode() == 127 || e.getKeyCode() == 8)
                        {
                            System.out.println("Delete key pressed for wall");
                            simulation.removeObservable((Observable)rigidBody);
                            remove(((java.awt.Component) (rigidBodyView)));
                            System.out.println("Rigid body view removed");
                            revalidate();
                            repaint();
                        }
                    }

                }
)));
                observables[i].addObserver(((java.util.Observer) (rigidBodyView)));
                add(((java.awt.Component) (rigidBodyView)));
            }

        sensorPanels.add(((java.awt.Component) (new SensorPanel())), "TEMP");
        revalidate();
        repaint();
    }

    public String getNewCardString()
    {
        return "robot" + robotIndex++;
    }

    public JPanel getSensorPanels()
    {
        return sensorPanels;
    }


}
